/*
 * Copyright 2010 jun.enomoto<jun.enomoto@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package foursquare4j.xml.handler;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

import foursquare4j.type.Badge;
import foursquare4j.type.Badges;
import foursquare4j.type.Checkin;
import foursquare4j.type.Settings;
import foursquare4j.type.Status;
import foursquare4j.type.User;
import foursquare4j.type.Venue;

public class UserHandler extends Handler<User> {

	private static enum E {
		_, CHECKIN, CHECKIN_VENUE, BADGES, BADGES_BADGE, SETTINGS, STATUS
	}

	private E e;

	private User user;
	private Badge badge;

	@Override
	public void startDocument() throws SAXException {
		user = new User();
	}

	@Override
	public void startElement(final String uri, final String localName, final String qName, final Attributes attributes) throws SAXException {
		super.startElement(uri, localName, qName, attributes);
		if ("user".equals(qName)) e = E._;
		else if ("checkin".equals(qName) && e == E._) {
			e = E.CHECKIN;
			user.checkin = new Checkin();
		} else if ("venue".equals(qName) && e == E.CHECKIN) {
			e = E.CHECKIN_VENUE;
			user.checkin.venue = new Venue();
		} else if ("badges".equals(qName) && e == E._) {
			e = E.BADGES;
			user.badges = new Badges();
		} else if ("badge".equals(qName) && e == E.BADGES) {
			e = E.BADGES_BADGE;
			user.badges.add(badge = new Badge());
		} else if ("settings".equals(qName) && e == E._) {
			e = E.SETTINGS;
			user.settings = new Settings();
		} else if ("status".equals(qName) && e == E._) {
			e = E.STATUS;
			user.status = new Status();
		}
	}

	@Override
	public void endElement(final String uri, final String localName, final String qName) throws SAXException {
		if ("user".equals(qName) && e == E._) e = null;
		else if ("checkin".equals(qName) && e == E.CHECKIN) e = E._;
		else if ("venue".equals(qName) && e == E.CHECKIN_VENUE) e = E.CHECKIN;
		else if ("badges".equals(qName) && e == E.BADGES) e = E._;
		else if ("badge".equals(qName) && e == E.BADGES_BADGE) e = E.BADGES;
		else if ("settings".equals(qName) && e == E.SETTINGS) e = E._;
		else if ("status".equals(qName) && e == E.STATUS) e = E._;
	}

	@Override
	protected void characters(final String text) throws SAXException {
		switch (e) {
			case _:
				set(user, tag, text);
				break;
			case CHECKIN:
				set(user.checkin, tag, text);
				break;
			case CHECKIN_VENUE:
				set(user.checkin.venue, tag, text);
				break;
			case BADGES:
				break;
			case BADGES_BADGE:
				set(badge, tag, text);
				break;
			case SETTINGS:
				set(user.settings, tag, text);
				break;
			case STATUS:
				set(user.status, tag, text);
				break;
		}
	}

	@Override
	public User getObject() {
		return user;
	}
}